package com.grechur.wanandroidcompose.viewmodel

import android.app.Application
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.grechur.wanandroidcompose.bean.article.Article
import com.grechur.wanandroidcompose.bean.article.PageData
import com.grechur.wanandroidcompose.http.NetworkRequest
import com.grechur.wanandroidcompose.http.service.ProjectService
import com.grechur.wanandroidcompose.widget.smartrefresh.SmartSwipeRefreshState

class ProjectArticleViewModel(application: Application) : BaseViewModel(application){
//    val repository: ProjectRepository = ProjectRepository()
    var projectArticles by mutableStateOf(Result.success(listOf<Article>()))
        private set
    var projectArticlesPageIndex = mutableStateOf(0)
        private set
    var isPageOver by mutableStateOf(false)
        private set
    suspend fun getProjectArticles(pageIndex:Int = 1,cid:String) {
        getProjectArticles(pageIndex, cid,null)
    }
     suspend fun getProjectArticles(pageIndex:Int = 1,cid:String,state:SmartSwipeRefreshState?) {
         try {
             val page = projectArticlesPageIndex.value
             if (!isPageOver) {
                 val articlesBeans = NetworkRequest.getService(getApplication(), ProjectService::class.java).getProjectArticle(pageIndex, cid)
                 Log.e("zhouzhu","getProjectArticles $cid articles:${articlesBeans?.data?.size}")
                 state?.isRefreshing = false
                 state?.isLoading = false

                 articlesBeans.takeIf { it != null }?.let {articlesBean->
                     articlesBean.errorCode.takeIf { it == 0 }?.let { i ->
                         articlesBean.data.takeUnless { it == null }?.let { data: PageData ->
                             isPageOver = data.over?:true
                             data.datas.takeIf { !it.isNullOrEmpty() }
                                 ?.let { articleList ->
                                     projectArticles = if (pageIndex == 1 || page == pageIndex) {
                                         Result.success(articleList)
                                     }else{
                                         val data = projectArticles.getOrDefault(listOf())
                                         val all = mutableListOf<Article>()
                                         all.addAll(data)
                                         all.addAll(articleList)
                                         Result.success(all)
                                     }
                                     projectArticlesPageIndex.value = pageIndex
                                 }
                         }
                     }
                 }
             }
         } catch (e: Exception) {
             projectArticles = Result.failure(e)
         }
    }

    override fun upItemData(index:Int,isAdd:Boolean) {
        val list = projectArticles.getOrDefault(listOf())
        if (index >= 0 && index < list.size) {
            val mutableListOf = list.toMutableList()
            val article = mutableListOf.removeAt(index).copy()
            article.collect = isAdd
            mutableListOf.add(index, article)
            projectArticles = Result.success(mutableListOf.toList())
        }
    }
}